//
//  MEStageTabItemView.m
//  MapEditor
//
//  Created by Eric on 3/7/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MEStageTabItemView.h"
#import "MEGLView.h"
#import "MEController.h"

@implementation MEStageTabItemView
@synthesize ptmRatioField;
@synthesize grativeXField;
@synthesize grativeYField;
@synthesize sidCountField;

@synthesize tag0Field;
@synthesize tag1Field;
@synthesize tag2Field;
@synthesize tag3Field;

-(void)dealloc{
    self.ptmRatioField = nil;
    self.grativeXField = nil;
    self.grativeYField = nil;
    self.sidCountField = nil;
    self.tag0Field = nil;
    self.tag1Field = nil;
    self.tag2Field = nil;
    self.tag3Field = nil;
    
    [super dealloc];
}

-(IBAction)worldFieldChanged:(id)sender
{
    CGFloat ptm = ptmRatioField.floatValue;
    CGFloat gx = grativeXField.floatValue;
    CGFloat gy = grativeYField.floatValue;
    
    [[MEController instance].glView.editStageLayer resetWorld:ptm gravityX:gx gravityY:gy];
    
    //[field setRefusesFirstResponder:YES];
    [[self window] makeFirstResponder:self];
}
-(IBAction)tagFieldChanged:(id)sender
{
    NSInteger tag0 = tag0Field.integerValue;
    NSInteger tag1 = tag1Field.integerValue;
    NSInteger tag2 = tag2Field.integerValue;
    NSInteger tag3 = tag3Field.integerValue;
    
    [MEController instance].glView.editStageLayer.tag0 = tag0;
    [MEController instance].glView.editStageLayer.tag1 = tag1;
    [MEController instance].glView.editStageLayer.tag2 = tag2;
    [MEController instance].glView.editStageLayer.tag3 = tag3;
}

@end



@implementation MEStageTabDelegate

- (void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
    
}

- (BOOL)tabView:(NSTabView *)tabView shouldSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
    [[tabView window] makeFirstResponder:tabView];
    MEGLView* glView = (MEGLView*)[CCDirector sharedDirector].openGLView;
    [glView.editStageLayer changeStage:[tabViewItem.label integerValue]-1];
    
    MEStageTabItemView* itemView = tabViewItem.view;
    itemView.ptmRatioField.floatValue = glView.editStageLayer.ptmRatio;
    itemView.grativeXField.floatValue = glView.editStageLayer.grative.x;
    itemView.grativeYField.floatValue = glView.editStageLayer.grative.y;
    itemView.sidCountField.integerValue = glView.editStageLayer.spriteSidCount;
    
    itemView.tag0Field.integerValue = glView.editStageLayer.tag0;
    itemView.tag1Field.integerValue = glView.editStageLayer.tag1;
    itemView.tag2Field.integerValue = glView.editStageLayer.tag2;
    itemView.tag3Field.integerValue = glView.editStageLayer.tag3;
    
    return YES;
}

- (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem
{
}

- (void)tabViewDidChangeNumberOfTabViewItems:(NSTabView *)tabView
{
    
}

@end